const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require("path");
const minify = require('html-minifier').minify;
module.exports = {
    output: {
        filename: "scripts/[name].[hash:5].bundle.js"
    },
    plugins: [
        new CopyWebpackPlugin(
            [{
                //from: path.join(__dirname, "../" + "/src/webapp/views/common/layout.html"),
                from: path.join(__dirname, "../src/webapp/views/common/layout.html"),
                to: "../views/common/layout.html"
            }]),
        // new CopyWebpackPlugin([{
        //     from: "./src/webapp/views/common",
        //     to: "../views/common"
        // },{
        //     from: "./src/webapp/widgets/**/*.html",//如果直接加**/* 会拷贝全目录
        //     to: "../widgets/[name]/[name].html",
        //     toType: 'template'
        // }]),
        new CopyWebpackPlugin(
            [{
                //from: path.join(__dirname, "../" + "src/webapp/widgets/"),
                from: path.join(__dirname, "../src/webapp/widgets/"),
                to: "../widgets",
                transform(content, path) {
                    let resultContent = content;

                    // 仅对html文件进行html压缩
                    if (/.+\.html$/g.test(path)) {
                        resultContent = minify(content.toString('utf-8'), {
                            collapseWhitespace: true
                        });
                    }

                    return resultContent;
                }
            }], {
                ignore: ['*.js', '*.css'],
            }),
        new ExtractTextPlugin({
            filename: "styles/[name][hash:5].css",
            allChunks: true
        }),
    ],
}
